//
//  JFBSubmitOrderDS.m
//  ios-supplier
//
//  Created by Benlai on 15/8/19.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBSubmitOrderDS.h"
#import "JFBOrderProductDS.h"
#import "ApiPurchaseDetail.h"
/*
 ///采购单ID
 @property (nonatomic,assign) NSInteger ID;
 ///采购单内部编码
 @property (nonatomic,  copy) NSString *purchaseNoInside;
 ///采购单外部编码
 @property (nonatomic,  copy) NSString *purchaseNoOutside;
 ///商家id
 @property (nonatomic,assign) NSInteger merchantId;
 ///商家名称
 @property (nonatomic,  copy) NSString *merchantName;
 ///仓库ID
 @property (nonatomic,assign) NSInteger warehouseId;
 ///仓库名称
 @property (nonatomic,  copy) NSString *warehouseName;
 ///供应商ID
 @property (nonatomic,assign) NSInteger supplierId;
 ///供应商名称
 @property (nonatomic,  copy) NSString *supplierName;
 ///供应商联系人
 @property (nonatomic,  copy) NSString *supplierContactPerson;
 ///供应商联系人电话
 @property (nonatomic,  copy) NSString *supplierContactPhone;
 ///送货方式编码
 @property (nonatomic,assign) NSInteger deliveryType;
 ///送货方式
 @property (nonatomic,  copy) NSString *deliveryTypeText;
 ///采购类型编码
 @property (nonatomic,assign) NSInteger purchaseType;
 ///采购类型文字
 @property (nonatomic,  copy) NSString *purchaseTypeText;
 ///备注
 @property (nonatomic,  copy) NSString *note;
 ///采购单状态编码
 @property (nonatomic,assign) NSInteger purchaseStatus;
 ///采购单状态文字
 @property (nonatomic,  copy) NSString *purchaseStatusText;
 ///采购单金额
 @property (nonatomic,assign) NSInteger purchasePrice;
 ///采购单SKU
 @property (nonatomic,assign) NSInteger purchaseSku;
 ///采购单Units
 @property (nonatomic,assign) NSInteger purchaseUnits;
 ///采购来源文字
 @property (nonatomic,  copy) NSString *purchaseSourceText;
 ///审核通过/拒绝日期
 @property (nonatomic,  copy) NSString *auditDate;
 ///支付方式编码
 @property (nonatomic,assign) NSInteger payType;
 ///支付方式文字
 @property (nonatomic,  copy) NSString *payTypeText;
 ///下单人ID
 @property (nonatomic,assign) NSInteger createUserid;
 ///下单人姓名
 @property (nonatomic,  copy) NSString *createUsername;
 ///下单日期
 @property (nonatomic,  copy) NSString *createTime;
 ///采购单条目总数
 @property (nonatomic,assign) NSInteger purchaseEntriesTotal;
 ///采购产品编码
 @property (nonatomic,  copy) NSString *productCode;
 ///采购产品名称
 @property (nonatomic,  copy) NSString *productName;
 ///采购产品规格
 @property (nonatomic,  copy) NSString *productSpec;
 ///采购产品进价
 @property (nonatomic,assign) NSInteger productPrice;
 ///采购产品条形码
 @property (nonatomic,  copy) NSString *productBarcode;
 ///采购数量
 @property (nonatomic,assign) NSInteger purchaseCount;
 ///采购产品税率
 @property (nonatomic,  copy) NSString *taxRate;
 ///采购产品类目ID
 @property (nonatomic,assign) NSInteger productCategoryId;
 ///采购产品类目名称
 @property (nonatomic,  copy) NSString *productCategoryName;
 ///采购产品品牌ID
 @property (nonatomic,assign) NSInteger productBrandId;
 ///采购产品品牌名称
 @property (nonatomic,  copy) NSString *productBrandName;
 ///商品图片地址
 @property (nonatomic,  copy) NSString *photoUrl;
 ///是否精品
 @property (nonatomic,assign) NSInteger isHighQuality;
 */
@implementation JFBSubmitOrderDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.purchaseNoInside       = [[attributes objectForKey: @"purchaseNoInside"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"purchaseNoInside"];
    self.note       = [[attributes objectForKey: @"note"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"note"];
    self.purchaseStatus       = [[attributes objectForKey: @"purchaseStatus"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"purchaseStatus"] integerValue];
    self.purchasePrice         = [[attributes objectForKey: @"purchasePrice"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"purchasePrice"];
    self.purchaseUnits     = [[attributes objectForKey: @"purchaseUnits"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"purchaseUnits"] integerValue];
    self.createTime = [[attributes objectForKey: @"createTime"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"createTime"];
    self.ID              = [[attributes objectForKey: @"id"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"id"]  integerValue];
 
    NSArray *productArray= [attributes objectForKey:@"purchaseEntriesArray"];
    
    self.shopSOItem = [NSMutableArray array];
    for (NSDictionary *dic in productArray) {
        JFBOrderProductDS *product = [[JFBOrderProductDS alloc] initWithAttributes:dic];
        [self.shopSOItem addObject: product];
    }
    return self;
}


+ (NSURLSessionDataTask *)submitOrder:(void (^)(JFBSubmitOrderDS *orderDetailInfo, NSError *error))block shopInfo:(NSDictionary *)shopInfo
{
 
    return [[JFBAPIClient sharedClient: YES] GET: @"/opms-api/purchaseOrder.do" parameters: shopInfo success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@", JSON);
        if( [JSON[@"code"] integerValue] == 0)
        {
            [[[ApiPurchaseDetail alloc]initWithUt:[UserInfo currentUser].ut andWithPurchaseID:[JSON[@"data"] integerValue]]startWithCompletionSuccess:^(id JSON, YTKBaseRequest *request) {
                NSLog(@"%@",JSON);
                if ([JSON[@"code"] integerValue] == 0) {
                    JFBSubmitOrderDS *orderDetailInfo = [[JFBSubmitOrderDS alloc]initWithAttributes:JSON[@"data"]];
                    orderDetailInfo.resultCode = [JSON[@"code"] unsignedIntValue];
                    orderDetailInfo.message = [JSON[@"message"]isKindOfClass:[[NSNull null] class]]?@"":JSON[@"message"];
                    block(orderDetailInfo,nil);
                }else if ([JSON[@"code"] integerValue] == 99){
                    UIViewController* viewController;
                    UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                    viewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Login"];
                }else{
                    
                }
            } failure:^(YTKBaseRequest *request) {
                
            }];
//            JFBSubmitOrderDS *orderDetailInfo = [[JFBSubmitOrderDS alloc] initWithAttributes: [JSON objectForKey: @"data"]];
//            orderDetailInfo.resultCode = [JSON[@"code"] unsignedIntValue];
//            orderDetailInfo.message = [JSON[@"message"] isKindOfClass:[[NSNull null] class]] ? @"" : JSON[@"message"];
//            block(orderDetailInfo, nil);
        }
        else
        {
            block(nil, nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
}
+ (NSURLSessionDataTask *)loadMenuList:(void(^)(NSArray *menuList, NSError *error)) block andWithUT:(NSString *)ut andWithMerchantId:(NSInteger)merchantId andWithPurchaseStatus:(NSInteger)purchaseStatus andWithCurrentPage:(NSInteger)currentPage andWithItemsPerPage:(NSInteger)itemsPerPage andWithOrderItemNum:(NSInteger)orderItemNum
{
    
    NSDictionary *parameters = @{@"ut":ut,
                                 @"merchantId":@(merchantId),
                                 @"purchaseStatus":@(purchaseStatus),
                                 @"currentPage":@(currentPage),
                                 @"itemsPerPage":@(itemsPerPage)
                                 };
    
    
    return [[JFBAPIClient sharedClient: YES] GET:@"/opms-api/queryPurchaseList.do" parameters:parameters success:^(NSURLSessionDataTask * __unused task, id JSON) {
        
        if ([JSON[@"code"] integerValue] == 0) {
            NSMutableArray *result = [@[] mutableCopy];
            NSDictionary *subDic = [JSON valueForKeyPath:@"data"];
            if (![[subDic valueForKey:@"rows"]isEqual:[NSNull null]]) {
                NSArray *dataArray = [subDic valueForKey:@"rows"];
                for( NSDictionary *product in dataArray) {
                    JFBSubmitOrderDS * ds = [[JFBSubmitOrderDS alloc] initWithAttributes:product];
                    [result addObject:ds];
                }
                
            }
            
            block([NSArray arrayWithArray:result], nil);
        }else if ([JSON[@"code"] integerValue] == 99){
            UIViewController* viewController;
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            viewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Login"];
        }else {
            block(nil, nil);
        }
        
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, nil);
    }];
}
@end
